<?php
class friendsController extends userController{
    
    public function index() {
        //分页参数
        $url=url('friends/index',array('page'=>'{page}','type'=>intval($_GET['tpye'])));
        $limit = $this->pageLimit($url, 20);
        //列表信息
        $this->list=model('friends')->friends_list($limit,$this->user_id);
        $count=model('friends')->count($this->user_id);
        $this->page=$this->pageShow($count);
        $this->show();  
    }

    //添加
    public function add() {
        $fid=intval($_GET['fid']);
        if(empty($fid)){
            $this->alert('通讯获取错误！');
        }
        $info=model('user_list')->info($fid);
        if($info['uid']==$this->user_id){
            $this->alert('您无法将自己添加为联系人！');
        }
        //重复联系人
        if(model('friends')->info($this->user_id,$fid)){
            $this->alert('您已经添加过该联系人，无法重复添加！');
        }
        
        $this->info=$info;
        $this->show();
    }

    public function add_save() {
        $data=in($_POST);
        if(empty($data['fid'])){
            $this->msg('通讯获取错误',0);
        }
        if($data['fid']==$this->user_id){
            $this->msg('您无法将自己添加为联系人！',0);
        }
        //重复联系人
        if(model('friends')->info($this->user_id,$data['fid'])){
            $this->msg('您已经添加过该联系人，无法重复添加！',0);
        }

        $data['uid']=$this->user_id;
        model('friends')->add($data);
        $this->msg('联系人添加成功');
    }

    //修改
    public function edit() {
        $fid=$_GET['fid'];
        if(empty($fid)){
            $this->alert('通讯获取错误！');
        }
        if(!model('friends')->info($this->user_id,$fid)){
            $this->alert('该用户不是您的联系人！');
        }
        $this->info=model('user_list')->info($fid);
        $this->show(); 
    }

    public function edit_save() {
        $data=in($_POST);
        if(empty($data['fid'])){
            $this->msg('通讯获取错误',0);
        }
        if(!model('friends')->info($this->user_id,$data['fid'])){
            $this->msg('该用户不是您的联系人！',0);
        }
        $data['uid']=$this->user_id;
        model('friends')->edit($data);
        $this->msg('备注修改成功');
    }

    //删除
    public function del() {
        $fid=intval($_POST['fid']);
        if(empty($fid)){
            $this->msg('通讯获取错误',0);
        }
        //录入模型处理
        model('friends')->del($this->user_id,$fid);
        $this->msg('联系人解除成功！',1);
    }


}